﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace II2svn
{
    public partial class BitMap : UserControl
    {
        bool ready;
        int rank;
        bool[,] matrix;
        Color selcol;
        int cx, cy;
        int ix, iy;

        public BitMap()
        {
            InitializeComponent();
            selcol = Color.FromArgb(128,Color.Blue);
        }

        public void Init(int rank)
        {
            this.rank = rank;
            matrix = new bool[rank,rank];
            ready = true;
        }

        public List<int> ToList(){
            List<int> list = new List<int>();
            for (int i = 0; i < rank; i++)
            {
                for (int j = 0; j < rank; j++)
                {
                    list.Add(matrix[i, j] ? 1 : 0);
                }
            }

            return list;
        }

        public void FromList(List<int> list)
        {
            
            for (int i = 0; i < rank; i++)
            {
                for (int j = 0; j < rank; j++)
                {
                    matrix[i, j] = list[i*rank+j]==1?true:false;
                    
                }
            }

            Invalidate();
        }

        private void BitMap_Paint(object sender, PaintEventArgs e)
        {
            if (!ready)
            {
                e.Graphics.DrawString("not ready", new Font("Courier", 12), Brushes.Black, new PointF(10, 10));
            }
            else
            {
                float sh = (float)this.Height/(float)rank;
                float sw = (float)this.Width/(float)rank;

                for (int i = 0; i < rank; i++)
                    for (int j = 0; j < rank; j++)
                {
                        Brush sb;
                        if(matrix[i,j]) sb = Brushes.Black;
                        else sb = Brushes.Wheat;

                    e.Graphics.FillRectangle(sb,new RectangleF(i*sw,j*sh,sw,sh));
                   // e.Graphics.DrawRectangle(Pens.Gray, new Rectangle((int)(i * sw), (int)(j * sh), (int)(sw),(int)(sh)));
                    e.Graphics.DrawRectangle(Pens.Gray, i * sw, j * sh, sw, sh);

                    ix = (int)(cx / sw);
                    iy = (int)(cy / sh);

                    if ( ix == i && iy == j)
                    {
                        e.Graphics.FillRectangle(new SolidBrush(selcol), new RectangleF(i * sw, j * sh, sw, sh));
                    }
                }

            }
        }

        private void BitMap_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void BitMap_MouseMove(object sender, MouseEventArgs e)
        {
            Invalidate();
            cx = e.X;
            cy = e.Y;
        }

        private void BitMap_Click(object sender, EventArgs e)
        {
            matrix[ix, iy] = !matrix[ix, iy];
        }

        private void BitMap_Load(object sender, EventArgs e)
        {

        }
    }
}
